<?php

use yii\helpers\Url;

$web = Url::base();

$site_name = \app\models\Mall::getCurrentInstitutionSiteName();
$this->title = $site_name . '-地址管理';
?>

<link rel="stylesheet" type="text/css" href="<?= $web ?>/css/mall-m/user-address-list.css"/>

<style type="text/css">
    .btn .choice-checkbox {
        background: url(<?= $web ?>/images/mall-m/choice-1.png) no-repeat;
        background-size: 100% 100%;
    }

    .btn .choice-checkbox:checked {
        background: url(<?= $web ?>/images/mall-m/pitch-1.png) no-repeat;
        background-size: 100% 100%;
    }
</style>

<div class="main">
    <div class="user-address">
        <?php
        foreach ($items as $item) {
            $isDefault = '';
            if ($item['is_default'] == 1) {
                $isDefault = 'checked';
            }
            ?>
            <div class="user-address-1">
                <div class="goods-receipt">
                    <div class="name-num">
                        <p><span class="name"><?= $item['name'] ?></span><span class="phone-num"><?= $item['phone'] ?></span></p>
                    </div>
                    <div class="address">
                        <p><?= $item['province_name'] . $item['city_name'] . $item['region_name'] . $item['address'] ?></p>
                    </div>
                </div>
                <div class="btn">
                    <div class="btn-1 f-l">
                        <label class="checkbox f-l"><input class="choice-checkbox" name="address_default" data-address-id="<?= $item['id'] ?>" type="radio" <?= $isDefault ?>>设为默认</label>
                    </div>
                    <a href="javascript:void(0);" class="btn-delete-address" data-address-id="<?= $item['id'] ?>">
                        <div class="btn-2 f-r">
                            <img class="f-l" src="<?= $web ?>/images/mall-m/delete-icon.png"/>
                            <p>删除</p>
                        </div>
                    </a>
                    <a href="user-address?address_id=<?= $item['id'] ?>">
                        <div class="btn-3 f-r">
                            <img class="f-l" src="<?= $web ?>/images/mall-m/compile-icon.png"/>
                            <p>编辑</p>
                        </div>
                    </a>
                </div>
            </div>
            <?php
        }
        ?>
    </div>
    <script type="text/javascript">
        $(function () {
            $('.choice-checkbox').change(function () {
                var addressId = $(this).data('address-id');
                requestJson('set-default-user-address-handler', {address_id: addressId}, true, function (responseData) {
                    if (responseData.status == 0) {
                        <?php
                        if ($isFromOrder == 0) {
                            echo 'window.location.reload();';
                        } else {
                            echo 'self.location=document.referrer;';
                        }
                        ?>
                    } else {
                        alert(responseData.message);
                    }
                });
            });

            $('.btn-delete-address').click(function () {
                var r = confirm('确认删除此地址?');
                if (r == true) {
                    requestJson('delete-user-address-handler', {address_id: $(this).data('address-id')}, true, function (responseData) {
                        if (responseData.status == 0) {
                            window.location.reload();
                        } else {
                            alert('删除地址失败!');
                        }
                    });
                }
            });
        })
    </script>
    <a href="user-address">
        <div class="append-btn">
            <p>新建收货地址</p>
        </div>
    </a>

</div>
